<?php

class FEXCEL {
    var  $buffer, $row, $col ;

    function FEXCEL() {
       $this->buffer = '';
    }

    function Header() {
 	   //To be implemented in your own inherited class
    }

    function Open () {
       $this->buffer = pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
       $this->row = 0;
       $this->col = 0;
       $this->Header();
    }

    function Cell ($conteudo) {
       if ($this->_numerico($conteudo)) {
          $conteudo = ereg_replace('\,','.',ereg_replace('\.','',$conteudo));
          $this->buffer .= pack("sssss", 0x203, 14, $this->row, $this->col, 0x0);
          $this->buffer .= pack("d", $conteudo );
       }
       else {
          $l = strlen($conteudo);
          $this->buffer .= pack("ssssss", 0x204, 8 + $l, $this->row, $this->col, 0x0, $l);
          $this->buffer .= $conteudo;
       }
       $this->col++;
    }

    function Ln () {
       $this->col = 0;
       $this->row++;
    }

    function Close () {
       $this->buffer .= pack("ss", 0x0A, 0x00);
       $this->row = 0;
       $this->col = 0;
    }

    function Output ($name='') {
       $this->Close();
       
       if($name=='')
          $name = 'relatorio';
          
       if(php_sapi_name()!='cli'){
          header('Content-Type: application/vnd.ms-excel');
          if (headers_sent())
             $this->Error('Some data has already been output to browser, can\'t send XLS file');
          header('Content-Length: '.strlen($this->buffer));
          header('Content-disposition: inline; filename="'.$name.'.xls"');
          header("Content-Transfer-Encoding: binary ");
       }
       echo $this->buffer;
    }

    function _numerico($conteudo) {
       $resultado = true;
       $tam = strlen($conteudo);
       if ($tam>0) {
          for ($i=0;$i<$tam;$i++) {
            if (!eregi("[0-9]|[,.]", substr($conteudo,$i,1)))
               $resultado = false;
          }
       }
       else
          $resultado=false;

       return $resultado;
    }
}
?>
